<template>
  <el-form
    :model="userPass"
    status-icon
    :rules="rules"
    ref="userForm"
    v-loading="loading"
    label-width="100px"
  >
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input
        type="password"
        v-model="userPass.oldPassword"
        placeholder="请输入旧密码"
      ></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input
        type="password"
        v-model="userPass.newPassword"
        placeholder="请输入新密码"
      ></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="_newPassword">
      <el-input
        type="password"
        v-model="userPass._newPassword"
        placeholder="请再次输入新密码"
      ></el-input>
    </el-form-item>

    <el-form-item style="text-align: center;">
      <el-button type="danger" class="submit_btn" @click="onCloseUserDialog"
        >取消</el-button
      >
      <el-button
        type="primary"
        class="submit_btn"
        @click="onSubmitUserInfo('userForm')"
        >提交</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import { userManage } from '@/api/index'

var baseUrl = process.env.VUE_APP_BASE_API

export default {
  name: '',
  componentName: '',
  data() {
    const password0 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (value.length < 5 || value.length > 16) {
          callback(new Error('请输入不少于六不超过十六位的密码'))
        } else {
          callback()
        }
      }
    }

    const password1 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.userPass.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      userPass: {
        oldPassword: '',
        newPassword: '',
        _newPassword: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: password0, trigger: 'blur' }
        ],
        _newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: password1, trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    visible: Boolean
  },
  watch: {
    visible(newV, oldV) {
      if (newV) {
        this.userPass = {
          oldPassword: '',
          newPassword: '',
          _newPassword: ''
        }
      }
    }
  },
  created() {},
  methods: {
    onCloseUserDialog() {
      this.$emit('submit', false)
    },
    onSubmitUserInfo(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.updatePassword()
        } else {
          return false
        }
      })
    },
    async updatePassword() {
      this.loading = true
      // let userPass = {
      //   oldPassword: this.userPass.oldPassword,
      //   newPassword: this.userPass.newPassword
      // }
      let userPass = `oldPassword=${this.userPass.oldPassword}&newPassword=${this.userPass.newPassword}`
      try {
        const pass = await userManage.updatePass(userPass)
        console.log(pass)
        this.loading = false
        if (!pass.data) {
          this.$message.success(pass.message + '即将跳转至登录页面')
          setTimeout(() => {
            this.$emit('submit', true)
          }, 1000)
        } else {
          this.$message.error(pass.message)
          this.$emit('submit', false)
        }
      } catch {
        this.$message.error('修改密码不成功')
        this.loading = false
        this.$emit('submit', false)
      }
    }
  }
}
</script>
